package karma.main;

import net.minecraft.src.forge.*;
import java.util.Random;
import net.minecraft.src.*;

public class ItemHellBow extends Item
{
    public ItemHellBow(int i)
    {
        super(i);
        maxStackSize = 1;
        setMaxDamage(384);
    }

    /**
     * called when the player releases the use item button. Args: itemstack, world, entityplayer, itemInUseCount
     */
    public void onPlayerStoppedUsing(ItemStack itemstack, World world, EntityPlayer entityplayer, int i)
    {
        if (ForgeHooks.onArrowLoose(itemstack, world, entityplayer, getMaxItemUseDuration(itemstack) - i))
        {
            return;
        }

        boolean flag = entityplayer.capabilities.isCreativeMode || EnchantmentHelper.getEnchantmentLevel(Enchantment.infinity.effectId, itemstack) > 0;

        if (flag || entityplayer.inventory.hasItem(Item.arrow.shiftedIndex))
        {
            int j = getMaxItemUseDuration(itemstack) - i;
            float f = (float)j / 20F;
            f = (f * f + f * 2.0F) / 3F;

            if ((double)f < 0.10000000000000001D)
            {
                return;
            }

            if (f > 1.0F)
            {
                f = 1.0F;
            }

            EntityArrow entityarrow = new EntityArrow(world, entityplayer, f * 2.0F);

            if (f == 1.0F)
            {
                entityarrow.arrowCritical = true;
            }

            int k = EnchantmentHelper.getEnchantmentLevel(Enchantment.power.effectId, itemstack) + 2;
            //grant 2 additional power
            if (k > 0)
            {
                entityarrow.setDamage(entityarrow.getDamage() + (double)k * 0.5D + 0.5D);
            }

            int l = EnchantmentHelper.getEnchantmentLevel(Enchantment.punch.effectId, itemstack);
            if (l > 0)
            {
                entityarrow.setKnockbackStrength(l);
            }
            //grant flame
            entityarrow.setFire(100);


            itemstack.damageItem(1, entityplayer);
            world.playSoundAtEntity(entityplayer, "random.bow", 1.0F, 1.0F / (itemRand.nextFloat() * 0.4F + 1.2F) + f * 0.5F);

            if (!flag)
            {
                entityplayer.inventory.consumeInventoryItem(Item.arrow.shiftedIndex);
            }
            else
            {
                entityarrow.doesArrowBelongToPlayer = false;
            }

            if (!world.isRemote)
            {
                world.spawnEntityInWorld(entityarrow);
            }
        }
    }

    public ItemStack onFoodEaten(ItemStack itemstack, World world, EntityPlayer entityplayer)
    {
        return itemstack;
    }

    /**
     * How long it takes to use or consume an item
     */
    @Override
    public int getMaxItemUseDuration(ItemStack itemstack)
    {
        return 0x11940;
    }

    /**
     * returns the action that specifies what animation to play when the items is being used
     */
    public EnumAction getItemUseAction(ItemStack itemstack)
    {
        return EnumAction.bow;
    }

    /**
     * Called whenever this item is equipped and the right mouse button is pressed. Args: itemStack, world, entityPlayer
     */
    public ItemStack onItemRightClick(ItemStack itemstack, World world, EntityPlayer entityplayer)
    {
        ItemStack itemstack1 = ForgeHooks.onArrowNock(itemstack, world, entityplayer);

        if (itemstack1 != null)
        {
            return itemstack1;
        }

        if (entityplayer.capabilities.isCreativeMode || entityplayer.inventory.hasItem(Item.arrow.shiftedIndex))
        {
            entityplayer.setItemInUse(itemstack, getMaxItemUseDuration(itemstack));
        }

        return itemstack;
    }

    /**
     * Return the enchantability factor of the item, most of the time is based on material.
     */
    public int getItemEnchantability()
    {
        return 1;
    }

    /**
     * Return an item rarity from EnumRarity
     */
    public EnumRarity getRarity(ItemStack itemstack)
    {
        return mod_KarmaCraft.ENCHANTED;
    }
    
    public void onUpdate(ItemStack itemstack, World world, Entity entity, int i, boolean flag)
    {
    	EntityPlayer entityplayer = (EntityPlayer)entity;
        ItemStack itemstack1 = entityplayer.inventory.getCurrentItem();
        if(entityplayer.isUsingItem() && itemstack1.itemID == mod_KarmaCraft.HellBow.shiftedIndex)
        {
        	int k = itemstack.getMaxItemUseDuration() - entityplayer.getItemInUseCount();
            if(k >= 18)
            {
                    iconIndex = mod_KarmaCraft.hellBow4;
            }
            else if(k > 13)
            {
                    iconIndex = mod_KarmaCraft.hellBow3;
            }
            else if(k > 0)
            {
                    iconIndex = mod_KarmaCraft.hellBow2;
            }
        }
        else
        {
            iconIndex = mod_KarmaCraft.hellBow;
        }
    }
}
